#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/generic/apis/cmsi/send_qy_weixin.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from django import forms
from components.component import Component, SetupConfMixin
from common.forms import BaseComponentForm, ListField
from .toolkit import configs

class SendQyWeixin(Component, SetupConfMixin):
    """
    apiLabel 发送企业微信
    apiMethod POST
    
    ### 功能描述
    
    发送企业微信
    
    ### 请求参数
    
    {{ common_args_desc }}
    
    #### 接口参数
    
    | 字段               |  类型      | 必选   |  描述      |
    |--------------------|------------|--------|------------|
    | receiver           |  string    | 是     | 微信接收者，包含企业微信用户ID，多个以逗号分隔 |
    | content            |  string    | 是     | 消息内容，长度最长为2048字符 |
    
    ### 请求参数示例
    
    ```python
    {
        "app_code": "esb_test",
        "app_secret": "xxx",
        "bk_token": "xxx",
        "receiver": "1234567890",
        "content": "This is a Test",
    }
    ```
    
    ### 返回结果示例
    
    ```python
    {
        "result": true,
        "code": "00",
        "message": "微信发送成功。",
    }
    ```
    """
    sys_name = configs.SYSTEM_NAME

    class Form(BaseComponentForm):
        receiver = ListField(label=u'接收者', required=True)
        content = forms.CharField(label=u'消息内容', required=True)

        def clean(self):
            return {'touser': self.cleaned_data['receiver'],
               'content': self.cleaned_data['content']
               }

    def handle(self):
        self.wx_qy_corpid = getattr(self, 'wx_qy_corpid', '') or getattr(configs, 'wx_qy_corpid', '')
        self.wx_qy_corpsecret = getattr(self, 'wx_qy_corpsecret', '') or getattr(configs, 'wx_qy_corpsecret', '')
        self.wx_qy_agentid = getattr(self, 'wx_qy_agentid', '') or getattr(configs, 'wx_qy_agentid', '')
        data = self.form_data
        data.update({'corpid': self.wx_qy_corpid,
           'corpsecret': self.wx_qy_corpsecret,
           'agentid': self.wx_qy_agentid
           })
        self.response.payload = self.invoke_other('generic.weixin_qy.send_message', kwargs=data)
